import cm.word.generate.App;
import cm.word.generate.dao.pgsql.PgsqlQueryMapper;
import cm.word.generate.pojo.PgsqlWordDto;
import cm.word.generate.utils.JacobOperaword;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author caomin
 * @date 2018/5/7
 * @说明 pgsql生成
 */
@SpringBootTest(classes = App.class)
@RunWith(value = SpringRunner.class)
@SuppressWarnings("all")
public class PgsqlTestWord {


    @Autowired
    PgsqlQueryMapper pgsqlQueryMapper;

    @Test
    public void test() throws Exception {
        JacobOperaword jacobOperaword = new JacobOperaword(true);
        //创建文档
        jacobOperaword.createNewDocument();
        jacobOperaword.moveDown(10);
        //自动获取所有表名列表
        List<String> tableList = pgsqlQueryMapper.getTables();

        //手动指定要生成的表名列表
       /* List<String> tableList = new ArrayList<>();
        tableList.add("drying_sensor_field_config");
        tableList.add("drying_sensor_field_config");
*/

        List<String> collect = tableList.stream().filter(t -> !t.startsWith("qrtz"))
                .filter(t -> !t.equalsIgnoreCase("spatial_ref_sys"))
                .collect(Collectors.toList());


        //所有表总览
        String firstTableName = "表清单";
        jacobOperaword.insertText(firstTableName);

        jacobOperaword.setTitle(firstTableName);

        jacobOperaword.moveDown(2);
        jacobOperaword.moveRight(2);
        //表的数量，加上表清单。所以要加1
        int tableNumber = collect.size() + 1;
        jacobOperaword.createTable(firstTableName, 3, tableNumber);


        //设置表头
        jacobOperaword.putTxtToCell(1, 1, 1, "序号");
        jacobOperaword.putTxtToCell(1, 1, 2, "表名");
        jacobOperaword.putTxtToCell(1, 1, 3, "注释");
        for (Integer i = 0; i < collect.size(); i++) {
            Integer tt = i + 1;
            jacobOperaword.putTxtToCell(1, i + 2, 1, tt.toString());
            jacobOperaword.putTxtToCell(1, i + 2, 2, collect.get(i));
            String tableComment = pgsqlQueryMapper.getTableComment(collect.get(i));
            jacobOperaword.putTxtToCell(1, i + 2, 3, tableComment);
        }


        //下移两行,否则表会连在一起不能创建新的表格
        jacobOperaword.moveDown(5);
        jacobOperaword.moveRight(6);

        StringBuffer stringBuffer = new StringBuffer();
        try {
            for (int i = 0; i < tableNumber; i++) {
                String tableName = null;
                try {
                    tableName = collect.get(i);

                    List<PgsqlWordDto> wordList = pgsqlQueryMapper.getColumnList(tableName);
                    String tableComment = pgsqlQueryMapper.getTableComment(tableName);
                    System.out.println(tableName);
                    int tableIndex = i + 2;
                    //创建表格的行数与列数
                    int cols = 7, rows = wordList.size() + 1;
                    String text = tableName + "表详细设计";
                    String newText = tableComment + "(" + tableName + ")";
                    jacobOperaword.insertText(newText);
                    jacobOperaword.setTitle(text);


                    jacobOperaword.moveDown(2);
                    jacobOperaword.moveRight(2);
                    jacobOperaword.createTable(text, cols, rows);

                    jacobOperaword.moveDown(2);
                    jacobOperaword.moveRight(2);
                    jacobOperaword.putTxtToCell(tableIndex, 1, 1, "序号");
                    jacobOperaword.putTxtToCell(tableIndex, 1, 2, "字段名");
                    jacobOperaword.putTxtToCell(tableIndex, 1, 3, "数据类型");
                    jacobOperaword.putTxtToCell(tableIndex, 1, 4, "是否为主键");
                    jacobOperaword.putTxtToCell(tableIndex, 1, 5, "是否非空约束");
                    jacobOperaword.putTxtToCell(tableIndex, 1, 6, "说明");
                    jacobOperaword.putTxtToCell(tableIndex, 1, 7, "默认值");
                    for (int j = 0; j < wordList.size(); j++) {
                        PgsqlWordDto pgsqlWordDto = wordList.get(j);
                        String columnName = pgsqlWordDto.getColumnName();
                        //表格的序号
                        jacobOperaword.putTxtToCell(tableIndex, j + 2, 1, String.valueOf(j+1));
                        //字段名称：name
                        jacobOperaword.putTxtToCell(tableIndex, j + 2, 2, columnName);
                        //字段类型
                        jacobOperaword.putTxtToCell(tableIndex, j + 2, 3, pgsqlWordDto.getColumnType());

                        //是否为主键
                        String isPrimarkey = "FALSE";
                        if (pgsqlWordDto.getColumnName().equalsIgnoreCase("id")) {
                            isPrimarkey = "TRUE";
                        }
                        jacobOperaword.putTxtToCell(tableIndex, j + 2, 4, isPrimarkey);

                        //是否非空约束
                        Boolean sfwk = pgsqlWordDto.isSfwk();
                        String sfwkString = "FALSE";
                        if (sfwk != null && sfwk == true) {
                            sfwkString = "TRUE";
                        }
                        jacobOperaword.putTxtToCell(tableIndex, j + 2, 5, sfwkString);
                        //字段说明：姓名
                        jacobOperaword.putTxtToCell(tableIndex, j + 2, 6, pgsqlWordDto.getBz());

                        //默认值
                        String defaultValue = pgsqlWordDto.getDefaultValue();
                        jacobOperaword.putTxtToCell(tableIndex, j + 2, 7, defaultValue);
                    }
                    jacobOperaword.moveDown(3);
                    System.out.println("成功生成:===========" + tableName);
                } catch (Exception e) {
                    System.out.println("生成失败:===========" + tableName);
                    e.printStackTrace();
                }
            }

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            jacobOperaword.save("d://qkl1.2.doc");
            jacobOperaword.setSaveOnExit(true);
            jacobOperaword.closeDocumentWithoutSave();
            jacobOperaword.closeDocument();
            jacobOperaword.close();
        }
    }


}



